package com.qf.agro.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.mapper.sys.CompanyMapper;
import com.qf.agro.pojo.sys.Company;
import com.qf.agro.pojo.sys.SysUser;
import com.qf.agro.service.sys.CompanyService;
import com.qf.agro.vo.OptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @company：千锋教育
 * @author：zhy
 * @date：2023/6/29
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Page<Company> findPage(Integer pageNum, Integer pageSize, String companyName) {
        //1.创建分页对象
        Page<Company> page = new Page<>(pageNum,pageSize);
        //2.创建查询条件对象
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        //3.设置条件
        queryWrapper.like(companyName != null,Company::getName,companyName);
        //4.分页查询
        page = companyMapper.selectPage(page,queryWrapper);
        //5.返回
        return page;
    }

    @Override
    public List<OptionVo> findOptions() {
        return companyMapper.selectOptions();
    }

    @Override
    public void save(Company company) {
        companyMapper.insert(company);
    }

    @Override
    public void updateState(Long id) {
        Company company = new Company();
        company.setState(0);
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getId,id);
        companyMapper.update(company,queryWrapper);//update user  set state = 0 where user_id = id;
    }

    @Override
    public void update(Company company) {
        companyMapper.updateById(company);
    }
}
